<?php
namespace App\Service\Inquiry;

use App\Models\Customer\CustomerModel;
use App\Models\Goods\GoodsModel;
use App\Models\Inquiry\InquirySheet;
use App\Models\Inquiry\InquirySheetItems;
use Illuminate\Support\Facades\DB;

class InquiryService
{
    /**
     * 存储询价单
     */
    public static function create($request,GoodsModel $goodsModel,CustomerModel $customerModel)
    {
        return InquirySheet::create([
            'uid' => $customerModel->uid,
            'user_nickname' => $customerModel->nickname,
            'goods_num' => $request->number,
            'no' => self::generateNo($customerModel->id),
            'notes' => $request->notes?$request->notes:'',
            'goods_id' => $goodsModel->id,
            'goods_name' => $goodsModel->name,
            'goods_spec' => $goodsModel->spec,
            'goods_thumb' => $goodsModel->thumb,
            'number' => $request->number
        ]);
    }

    /**
     *  生成询价单编号
     */
    public static function generateNo($id)
    {
        return time().$id;
    }

    /**
     *  根据用户uid获取询价单
     */
    public static function getLastInquirySheet($uid)
    {
        return InquirySheet::where('uid',$uid)->orderBy('created_at','desc')->first();
    }

    /**
     *  获得询价单
     */
    public static function getInquirySheet($uid,$status)
    {
        $inquiry = InquirySheet::where([
            'uid' => $uid,
            'status' => $status
        ])->with('quotation')->paginate(20);

        return $inquiry;
    }

    /**
     *  获得选价单
     */
    public static function oneInquirySheet($uid,$id)
    {
        return InquirySheet::where([
            'uid' => $uid,
            'id' => $id
        ])->whereIn('status',[0,1])->first();
    }

    /**
     *  获得所有的询价单
     */
    public static function getAllInquirySheet($status)
    {
        $inquiry = InquirySheet::where([
            'status' => $status
        ])->orderBy('id','desc')->with('quotation')->paginate(20);

        return $inquiry;
    }
}